<script setup>
  import { Delete, Edit } from '@element-plus/icons-vue'
  import { ref ,computed } from 'vue'
  import ViewUser from '@/components/shared/ViewUser.vue'
  import { getStudentListService, studentDelService, getClassListService } from '@/api/user'

  const loading = ref(false)
  const selectClass = ref(null)
  const drawer = ref()
  const userData = ref([])
  const selectedData = ref([])
  const classList = ref([])
  const pageSize=ref(15)
  const currentPage=ref(1)
  const getClassList = async () => {
    const res = await getClassListService()
    classList.value = res.data
  }
  getClassList()

  const getStudentList = async () => {
    loading.value = true
    const res = await getStudentListService()
    userData.value = res.data.student
    selectedData.value = userData.value;
    loading.value = false
  }
  getStudentList()

  const onEdit = (row) => {
    drawer.value.open(row)

  }
  const selectStudentList = () => {

    loading.value = true
    selectedData.value = userData.value.filter((item) => item.klass.code==selectClass.value.code)
    loading.value = false
  }
  const onDel = async (row) => {
    await ElMessageBox.confirm('你确认删除该学生信息吗？', '温馨提示', {
      type: 'warning',
      confirmButtonText: '确认',
      cancelButtonText: '取消'
    })
    await studentDelService(row._id)
    ElMessage({ type: 'success', message: '删除成功' })
    getStudentList()
  }
  const onSearch = () => {
    selectStudentList()
  }
  const onReset = () => {

    getStudentList()
  }
  const openDrawer = () => {
    drawer.value.open({})
  }

  const onSuccess = () => {
    getStudentList()
  }
  const paginatedData= computed(() => {
    const start = (currentPage.value - 1) * pageSize.value;
    const end = start + pageSize.value;
    return selectedData.value.slice(start, end);
});
  const handlePageChange=(pageNum) =>{
  currentPage.value=pageNum
  getStudentList()
  }
  const handleSizeChange=(size) =>{
      pageSize.value = size;
      getStudentList()
    }

</script>

<template>
  <page-container title="学生管理">
    <template #extra>
      <el-button type="primary" @click="openDrawer">添加学生</el-button>
    </template>
    <el-form inline>
      <el-form-item label="班级：">
        <el-select v-model="selectClass" style="width: 220px" placeholder="请选择班级" value-key="_id">
          <el-option v-for="item in classList" :key="item._id" :label='item.code' :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="paginatedData" style="width: 100%" v-loading="loading">
      <el-table-column label="班级">
        <template #default="scope">
          <span>{{scope.row.klass.code}}</span>
        </template>
      </el-table-column>
      <el-table-column label="学号" prop="code"></el-table-column>
      <el-table-column label="姓名" prop="name"></el-table-column>
      <el-table-column label="手机" prop="phone"></el-table-column>
      <el-table-column label="导师">
        <template #default="{ row }">
          {{ row.teacher ? row.teacher.name : '——' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button size="small" @click="onEdit(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="onDel(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <el-pagination background layout="total,prev, pager, next" :total="selectedData.length" @current-change="handlePageChange"  @size-change="handleSizeChange" :current-page="currentPage" :page-size="pageSize"
    style="margin-top: 20px; 
    justify-content: flex-end" />
    <view-user ref="drawer" @success="onSuccess"></view-user>
  </page-container>

</template>

<style scoped></style>